
package com.bei_en.teacher.management.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.management.model.system.bean.SystemRole;
import com.bei_en.teacher.management.model.system.bean.SystemRoleResource;
import com.bei_en.teacher.management.model.system.condition.SystemRoleCondition;
import com.bei_en.teacher.management.model.system.vo.SystemRoleVo;
import com.bei_en.teacher.management.persistence.dao.system.SystemRoleDao;
import com.bei_en.teacher.management.persistence.dao.system.SystemRoleResourceDao;
import com.bei_en.teacher.management.service.BaseService;

/**
 * 后台角色
 * @author Longqs
 * @date 2016-6-15 14:55:59
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class SystemRoleService extends BaseService {

    @Autowired
    private SystemRoleDao systemRoleDao;
    @Autowired
    private SystemRoleResourceDao systemRoleResourceDao;

    public void insert(SystemRoleVo model) {

	Integer merge = merge(model);
	if (model.getStatus()!=0) {// 编辑或删除
//	    sysRoleResourceDao.deletewhere(merge.getId());
	    String[] split = model.getResourceList().split(",");
	    for (int i = 0; i<split.length; i++) {
		SystemRoleResource mm = new SystemRoleResource();
		mm.setRoleId(merge);
		mm.setResourceId(Integer.valueOf(split[i]));
		systemRoleResourceDao.insert(mm);
	    }
	}
    }

    /**
     * 插入并返回生成的id
     * @param model
     * @return
     */
    public Integer merge(SystemRoleVo model) {

	systemRoleDao.insert(model.getSystemRole());
	return systemRoleDao.findLastInsertedId();

    }

    public void delete(Integer modelInteger) {

	systemRoleDao.delete(modelInteger);
    }

    public SystemRoleVo findById(Integer modelInteger) {

	SystemRoleVo model = new SystemRoleVo(systemRoleDao.findById(modelInteger));
	return model;
    }

    public void update(SystemRoleVo model) {

	systemRoleDao.update(model.getSystemRole());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(SystemRoleVo model) {

	systemRoleDao.updateSelective(model.getSystemRole());
	if (model.getStatus()!=0) {// 编辑或删除
	    systemRoleResourceDao.deletewhere(model.getId());
	    String[] split = model.getResourceList().split(",");
	    for (int i = 0; i<split.length; i++) {
		SystemRoleResource mm = new SystemRoleResource();
		mm.setRoleId(model.getId());
		mm.setResourceId(Integer.valueOf(split[i]));
		systemRoleResourceDao.insert(mm);
	    }
	}
    }

    public int countAll() {

	return systemRoleDao.countAll();
    }

    public List<SystemRoleVo> findAll() {

	List<SystemRoleVo> list = new ArrayList<SystemRoleVo>();
	List<SystemRole> list1 = systemRoleDao.findAll();
	for (SystemRole model : list1) {
	    list.add(new SystemRoleVo(model));
	}
	return list;
    }

    public List<Integer> findAllIds() {

	return systemRoleDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<SystemRoleVo> findSystemRolePage(SystemRoleCondition vo) {

	Integer totalCount = searchSystemRoleNum(vo);
	List<SystemRoleVo> list = new ArrayList<SystemRoleVo>();
	vo.setTotalCount(totalCount);

	if (totalCount>0) {
	    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "role_id");
	    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");

	    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
	    List<SystemRole> list1 = systemRoleDao.findPageBreakByCondition(vo, rb);
	    for (SystemRole model : list1) {
		list.add(new SystemRoleVo(model));
	    }

	}

	return list;
    }

    private Integer searchSystemRoleNum(SystemRoleCondition Vo) {

	return systemRoleDao.findNumberByCondition(Vo);
    }

    public Map<String, String> findSystemRolePageMap(SystemRoleCondition vo) {

	Integer totalCount = searchSystemRoleNum(vo);
	Map<String, String> map = new HashMap<String, String>();

	if (totalCount>0) {
	    vo.setTotalCount(totalCount);
	    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "role_id");
	    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");

	    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
	    List<SystemRole> list1 = systemRoleDao.findPageBreakByCondition(vo, rb);
	    for (SystemRole model : list1) {
		map.put(model.getRoleId().toString(), model.getRoleName());
	    }

	}

	return map;
    }

}
